<?php
/**
 * 自定义跳转类
 * User: fengyan
 * Date: 17-10-13
 * Time: 上午10:43
 */

namespace App\Utils\Helpers;


use App\Utils\Enums\CommonEnums;
use App\Utils\Enums\ExceptionEnums;


trait Jump
{
    /**
     * 模板前缀
     * @var string
     */
    protected $templatePrefix = '';

    /**
     * 返回数据格式
     * @var string
     */
    protected $returnFormat = 'json';

    /**
     * 返回的数据
     * @var array
     */
    protected $data = [];


    /**
     * 是否系统错误信息
     * @var bool
     */
    protected $isSystemError = false;

    /**
     * 返回json数据
     * @var string
     */
    public static $returnFormatJson = 'json';

    /**
     * 返回view页面
     * @var string
     */
    public static $returnFormatView = 'view';

    /**
     * 设置模板前缀
     * @param string $templatePrefix
     * @return $this
     */
    protected function setTemplatePrefix($templatePrefix = '')
    {
        $this->templatePrefix = $templatePrefix;

        return $this;
    }

    /**
     * 设置当前模块
     * @param string $returnFormat
     * @return $this
     */
    protected function setReturnFormat($returnFormat = 'json')
    {
        $this->returnFormat = $returnFormat;

        return $this;
    }

    public function withData($key, $value)
    {
        $this->data[$key] = $value;

        return $this;
    }

    /**
     * 设置是否系统错误信息
     * @param bool $flag
     * @return $this
     */
    protected function setIsSystemError($flag = false)
    {
        $this->isSystemError = $flag;
        return $this;
    }

    /**
     * 模板统一调用方法
     * @param null $view
     * @param array $data
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    protected function fetch($view = null, array $data = [])
    {
        if (strpos($view, '/') !== 0) {
            $template = $this->templatePrefix . $view;
        } else {
            $template = substr($view, 1);
        }

        $this->templatePrefix = '';

        return response()->view($template, array_merge($this->data, $data));
    }

    /**
     * 错误输出
     * @param string $msg
     * @param int $code
     * @param string $data
     * @param int $wait
     * @param int $httpStatus
     * @param array $header
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    protected function error($msg = '', array $data = [], $code = 0, $httpStatus = 200, array $header = [], $wait = 2)
    {
        $uriArr = explode( '/', trim(request()->getRequestUri(), '/'));
        if (!request()->ajax()) {
            if ($uriArr[0] == CommonEnums::MANAGE_MODULE_NAME) {
                $this->setReturnFormat(Jump::$returnFormatView);
            }
        }

        $code = $code === 0 ? ExceptionEnums::UNKNOWN_ERROR : $code;

        $result = array_merge($data, [
            'code' => $code,
            'msg' => $msg,
            'wait' => $wait,
        ]);

        if (stripos(request()->getUri(), CommonEnums::MANAGE_MODULE_NAME) !== false) {
            $httpStatus = ExceptionEnums::HTTP_CODE_LIST[$code]; // 得到对应的http状态码
        }

        if ($this->returnFormat == self::$returnFormatJson) {
            return response()->json($result, $httpStatus, $header);
        }

        return $this->fetch('/'.$uriArr[0] . '.error', array_merge($result, ['httpStatus' => $httpStatus]));
    }

    /**
     * 成功返回
     * @param string $msg
     * @param string $data
     * @param int $wait
     * @param int $httpStatus
     * @param array $header
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    protected function success($msg = '', array $data = [], $httpStatus = 200, array $header = [], $wait = 2)
    {
        $uriArr = explode( '/', trim(request()->getRequestUri(), '/'));
        if (!request()->ajax()) {
            if ($uriArr[0] == CommonEnums::MANAGE_MODULE_NAME) {
                $this->setReturnFormat(Jump::$returnFormatView);
            }
        }

        $httpStatus = empty($httpStatus) ? 200 : $httpStatus;
        $result = array_merge($data, [
            'code' => ExceptionEnums::SUCCESS,
            'msg' => $msg,
            'wait' => $wait,
        ]);

        if ($this->returnFormat == self::$returnFormatJson) {
            return response()->json($result, $httpStatus, $header);
        }

        return $this->fetch('/'.$uriArr[0] . '.success', array_merge($result, ['httpStatus' => $httpStatus]));
    }
}